package com.ruoyi.web.controller.ycdj;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TUploadconfig;
import com.ruoyi.system.service.TUploadconfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.AnnotationAJ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Api(tags = "文件上传")
@RestController
@RequestMapping("fileupload")
public class FileUploadController extends BaseController {

    @Autowired
            public TUploadconfigService tUploadconfigService;

    //上传配置列表
    @ApiOperation("上传配置列表")
    @GetMapping("getlist")
    public AjaxResult getlist(@RequestParam(value="pageNum",defaultValue = "")Integer pageNum,
                              @RequestParam(value="pageSize",defaultValue = "")Integer pageSize){

        if(pageNum!=null){
            PageHelper.startPage(pageNum,pageSize);
            List<TUploadconfig>tUploadconfigList=tUploadconfigService.list();
            PageInfo<TUploadconfig>pageInfo=new PageInfo<>(tUploadconfigList);
            return AjaxResult.success(pageInfo);
        }else{
            List<TUploadconfig>tUploadconfigList=tUploadconfigService.list();
            return AjaxResult.success(tUploadconfigList);
        }

    }

    //新增配置
    @ApiOperation("新增配置")
    @PostMapping("add")
    public AjaxResult add(@RequestBody TUploadconfig tUploadconfig){
        //判断是否有重复标记
        QueryWrapper<TUploadconfig>tUploadconfigQueryWrapper=new QueryWrapper<>();
        tUploadconfigQueryWrapper.eq("uploadflag",tUploadconfig.getUploadflag());
        if(tUploadconfigService.getOne(tUploadconfigQueryWrapper)!=null){
            return AjaxResult.warn("标记已存在");
        }
        return toAjax(tUploadconfigService.save(tUploadconfig));
    }

    //编辑配置
    @ApiOperation("编辑配置")
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TUploadconfig tUploadconfig){
        return  toAjax(tUploadconfigService.saveOrUpdate(tUploadconfig));
    }

    String rootPath = RuoYiConfig.getProfile();  //文件跟目录

    @ApiOperation("上传用户照片")
    @PostMapping("uploaduserpic")
    public AjaxResult uploaduserpic(@RequestParam("file") MultipartFile file){
        try {
            //获取文件名
            String fileName = file.getOriginalFilename();
            int lastindex = fileName.lastIndexOf('.');
            String[] split = fileName.split("\\.");
            String preFilename = fileName.substring(fileName.lastIndexOf('\\') + 1);
            String fileformat = fileName.substring(lastindex + 1);
            //设置文件上传后的路径
            String filePath = rootPath;
            String newname=DateUtils.dateTimeNow()+"."+fileformat;   //新名称，避免文件重名
            File dest = new File(filePath + "/userpic/" + newname);
            //检测目录是否存在
            if (!dest.getParentFile().exists()) {
                //文件不存在，创建新的文件防止异常发生
                dest.getParentFile().mkdirs();
            }
            //保存文件
            file.transferTo(dest);
            return AjaxResult.success( "profile/userpic/" + newname);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation("上传班子成员照片")
    @PostMapping("uploadteampic")
    public AjaxResult uploadteampic(@RequestParam("file") MultipartFile file){
        try {
            //获取文件名
            String fileName = file.getOriginalFilename();
            int lastindex = fileName.lastIndexOf('.');
            String[] split = fileName.split("\\.");
            String preFilename = fileName.substring(fileName.lastIndexOf('\\') + 1);
            String fileformat = fileName.substring(lastindex + 1);
            //设置文件上传后的路径
            String filePath = rootPath;
            String newname=DateUtils.dateTimeNow()+"."+fileformat;   //新名称，避免文件重名
            File dest = new File(filePath + "/teampic/" + newname);
            //检测目录是否存在
            if (!dest.getParentFile().exists()) {
                //文件不存在，创建新的文件防止异常发生
                dest.getParentFile().mkdirs();
            }
            //保存文件
            file.transferTo(dest);
            return AjaxResult.success( "profile/teampic/" + newname);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation("上传会议附件")
    @PostMapping("uploadmeetingfile")
    public AjaxResult uploadmeetingfile(@RequestParam("file") MultipartFile file){
        try {
            //获取文件名
            String fileName = file.getOriginalFilename();
            int lastindex = fileName.lastIndexOf('.');
            String[] split = fileName.split("\\.");
            String preFilename = fileName.substring(fileName.lastIndexOf('\\') + 1);
            String fileformat = fileName.substring(lastindex + 1);
            //设置文件上传后的路径
            String filePath = rootPath;
            String newname=DateUtils.dateTimeNow()+"."+fileformat;   //新名称，避免文件重名
            File dest = new File(filePath + "/meeting/" + newname);
            //检测目录是否存在
            if (!dest.getParentFile().exists()) {
                //文件不存在，创建新的文件防止异常发生
                dest.getParentFile().mkdirs();
            }
            //保存文件
            file.transferTo(dest);
            return AjaxResult.success( "profile/meeting/" + newname);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation("通用上传接口")
    @PostMapping("uploadfile")
    public AjaxResult uploadfile(@RequestParam("file") MultipartFile file,String flag){

        //根据flag获取配置文件路径
        QueryWrapper<TUploadconfig>tUploadconfigQueryWrapper=new QueryWrapper<>();
        tUploadconfigQueryWrapper.eq("uploadflag",flag);
        TUploadconfig tUploadconfig=tUploadconfigService.getOne(tUploadconfigQueryWrapper);

        try {
            //获取文件名
            String fileName = file.getOriginalFilename();
            int lastindex = fileName.lastIndexOf('.');
            String[] split = fileName.split("\\.");
            String preFilename = fileName.substring(fileName.lastIndexOf('\\') + 1);
            String fileformat = fileName.substring(lastindex + 1);
            //设置文件上传后的路径
            String filePath = rootPath;
            String newname=DateUtils.dateTimeNow()+"."+fileformat;   //新名称，避免文件重名
            File dest = new File(filePath + "/" + tUploadconfig.getUploadforld()+"/"+ newname);
            //检测目录是否存在
            if (!dest.getParentFile().exists()) {
                //文件不存在，创建新的文件防止异常发生
                dest.getParentFile().mkdirs();
            }
            //保存文件
            file.transferTo(dest);
            return AjaxResult.success( "profile/" +tUploadconfig.getUploadforld()+"/"+ newname);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
